package com.lishe.lishe.lishe.module.commonclass;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fynn.fluidlayout.FluidLayout;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.base.BaseActivity;
import com.lishe.lishe.lishe.module.mall.activity.MallSearchActivity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by shino on 2017/11/9.
 * 搜索页
 */

public class SearchActivity extends BaseActivity implements View.OnClickListener {
    private RelativeLayout callBack;
    private EditText editext;
    private ImageView cancle;
    private TextView search;
    private LinearLayout hostLayout;
    private LinearLayout historyLayout;
    private RecyclerView hostRecyclerView;
    private RecyclerView historyRecyclerView;
    private SearchHistoryAdapter historyAdapter;

    private boolean isJump = false;

    /**
     * @param activity
     * @param requesCode
     * @param isJump     是否需要跳转到搜索结果页
     */
    public static void jump(Activity activity, int requesCode, boolean isJump) {
        Intent intent = new Intent(activity, SearchActivity.class);
        intent.putExtra("isjump", isJump);
        activity.startActivityForResult(intent, requesCode);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        isJump = getIntent().getBooleanExtra("isjump", false);
        getRecorder();
        initView();
    }

    Set<String> set;

    private void initView() {
        callBack = findViewById(R.id.callBack);
        editext = findViewById(R.id.editext);
        cancle = findViewById(R.id.cancle);
        search = findViewById(R.id.search);
        hostLayout = findViewById(R.id.host_layout);
        historyLayout = findViewById(R.id.history_layout);
        hostRecyclerView = findViewById(R.id.history_recyclerView);
        historyRecyclerView = findViewById(R.id.history_recyclerView);
        //热搜接口未开发
        hostLayout.setVisibility(View.GONE);
        if (set != null && set.size() > 0) {
            List<String> searchText = new ArrayList<>(set);
            historyAdapter = new SearchHistoryAdapter(this, searchText);
            historyLayout.setVisibility(View.VISIBLE);
            historyRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(6, StaggeredGridLayoutManager.VERTICAL));
            historyRecyclerView.setAdapter(historyAdapter);
            historyAdapter.setOnItemClick(new SearchHistoryAdapter.HistoryItemOnclick() {
                @Override
                public void onItemClick(View v, String text) {
                    editext.setText(text);
                    editext.setSelection(text.length());
                }
            });
        } else {
            historyLayout.setVisibility(View.GONE);
        }
        setListener();
    }

    private void setListener() {
        callBack.setOnClickListener(this);
        cancle.setOnClickListener(this);
        search.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.callBack:
                finish();
                break;
            case R.id.cancle:
                editext.getText().clear();
                break;
            case R.id.search:
                String keyword = editext.getText().toString().trim();
                if (TextUtils.isEmpty(keyword)) {
                    editext.getText().clear();
                    toast("请输入您要搜索的商品!");
                    return;
                }
                setRecorder(keyword);

                if (isJump) {
                    MallSearchActivity.jump(SearchActivity.this, keyword);
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("keyword", editext.getText().toString());
                    setResult(RESULT_OK, intent);
                }
                finish();
                break;
        }
    }

    private void setRecorder(String string) {
        SharedPreferences recorder = getSharedPreferences("searchRecorder", MODE_PRIVATE);
        SharedPreferences.Editor editor = recorder.edit();
        if (set != null && set.size() < 61) {
            if (set.size() < 61) {

            } else {
                List<String> str = new ArrayList<>(set);
                set.remove(str.get(0));
            }
        } else {
            set = new HashSet();
        }
        set.add(string);
        editor.putStringSet("recorder", set);
        editor.commit();
    }

    private void getRecorder() {
        SharedPreferences recorder = getSharedPreferences("searchRecorder", MODE_PRIVATE);
        set = recorder.getStringSet("recorder", null);
        if (set == null) {
            Log.e("set", "null");
        } else {
            Log.e("set", "--" + set.size());
        }
    }
}
